package com.tuniu.agents.impl.hotel.logger;

import com.tuniu.agents.common.i18n.I18nConstants;
import com.tuniu.agents.impl.hotel.constants.HotelConstants;
import com.tuniu.agents.impl.hotel.enums.HotelToolEnum;
import com.tuniu.agents.message.MessageRender;
import com.tuniu.agents.message.ToolLogger;
import com.tuniu.agents.tools.hotel.tool.NlowestPriceHotelList;
import com.tuniu.agents.tools.hotel.tool.PlowestPriceHotelList;
import com.tuniu.agents.tools.hotel.utils.HotelCommonUtil;
import com.tuniu.agents.util.I18nUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service(HotelToolLoggingService.BEAN_NAME)
@Slf4j
public class HotelToolLoggingService implements ToolLogger {

    public static final String BEAN_NAME = "hotelToolLoggingService";
    @Autowired
    private MessageRender messageRender;
    @Override
    @SneakyThrows
    public List<String> before(Object request, ToolContext context) {
        if (Objects.isNull(request)) {
            return null;
        }
        String conversationId = (String)context.getContext().get(HotelConstants.CONVERSATION_ID);
        if (request instanceof NlowestPriceHotelList.Request poiRequest) {
            if (HotelCommonUtil.checkDateExpire(poiRequest.checkIn())) return null;
            String cityStr = StringUtils.hasText(poiRequest.cityName()) && !"null".equals(poiRequest.cityName()) ? poiRequest.cityName() : Strings.EMPTY;
            String markdownResponse = messageRender.renderTemplate(HotelConstants.THINKING_TOOL_SEARCH_TEMPLATE_NAME,
                    Map.of(I18nConstants.LANGUAGE_TAG, I18nUtil.getLocaleLanguageTag(conversationId)),
                    new Object[]{poiRequest.checkIn(), poiRequest.checkOut(), cityStr});
            String processResponse = messageRender.renderTemplate(HotelConstants.THINKING_TOOL_PROCESS_TEMPLATE_NAME,
                    Map.of(I18nConstants.LANGUAGE_TAG, I18nUtil.getLocaleLanguageTag(conversationId)),
                    new Object[]{});
            return List.of(markdownResponse, processResponse);
        } else if (request instanceof PlowestPriceHotelList.Request pkgRequest) {
            if (HotelCommonUtil.checkDateExpire(pkgRequest.checkIn())) return null;
            String cityStr = StringUtils.hasText(pkgRequest.cityName()) && !"null".equals(pkgRequest.cityName()) ? pkgRequest.cityName() : Strings.EMPTY;
            String markdownResponse = messageRender.renderTemplate(HotelConstants.THINKING_TOOL_SEARCH_TEMPLATE_NAME,
                    Map.of(I18nConstants.LANGUAGE_TAG, I18nUtil.getLocaleLanguageTag(conversationId)),
                    new Object[]{pkgRequest.checkIn(), pkgRequest.checkOut(), cityStr});
            String processResponse = messageRender.renderTemplate(HotelConstants.THINKING_TOOL_PROCESS_TEMPLATE_NAME,
                    Map.of(I18nConstants.LANGUAGE_TAG, I18nUtil.getLocaleLanguageTag(conversationId)),
                    new Object[]{HotelToolEnum.HOTEL_PACKAGE_LIST.getBefore()});
            return List.of(markdownResponse, processResponse);
        }
        return null;
    }

    @Override
    @SneakyThrows
    public List<String> after(Object request, ToolContext context, Object response) {
        if (Objects.isNull(response) || Objects.isNull(request)) {
            return null;
        }
        String conversationId = (String)context.getContext().get(HotelConstants.CONVERSATION_ID);
        if (response instanceof NlowestPriceHotelList.Response poiResponse) {
            if (CollectionUtils.isEmpty(poiResponse.data())
                    || HotelCommonUtil.checkDateExpire(((NlowestPriceHotelList.Request) request).checkIn())) {
                return null;
            }
            String markdownResponse = messageRender.renderTemplate(HotelConstants.THINKING_TOOL_FOUND_TEMPLATE_NAME,
                    Map.of(I18nConstants.LANGUAGE_TAG, I18nUtil.getLocaleLanguageTag(conversationId)),
                    new Object[]{poiResponse.data().size()});
            return List.of(markdownResponse);
        } else if (response instanceof PlowestPriceHotelList.Response pkgResponse) {
            if (Objects.isNull(pkgResponse.data())
                    || CollectionUtils.isEmpty(pkgResponse.data().rows())
                    || HotelCommonUtil.checkDateExpire(((PlowestPriceHotelList.Request) request).checkIn())) {
                return null;
            }
            String markdownResponse = messageRender.renderTemplate(HotelConstants.THINKING_TOOL_FOUND_TEMPLATE_NAME,
                    Map.of(I18nConstants.LANGUAGE_TAG, I18nUtil.getLocaleLanguageTag(conversationId)),
                    new Object[]{pkgResponse.data().rows().size(),HotelToolEnum.HOTEL_PACKAGE_LIST.getBefore()});
            return List.of(markdownResponse);
        }
        return null;
    }
}
